var express = require('express');
var router = express.Router();

var JPush = require("../lib/JPush.js");

//31.865710253529212"lon: "118.79163459677973 nanjing
//120.48181327404/30.753499067284 wuzhen
var lat_ = 1.69017867726027;
var lon_ = -1.112211186245212;
var driverAll = [{"driver":"司机1", "lat":"30.753499067284", "lon":"120.48181327404"},{"driver":"司机2", "lat":"30.753499067284", "lon":"120.48161327404"}];
var passengerAll = [{"passenger":"乘客1", "lat":"30.753299067284", "lon":"120.48181327404"},{"passenger":"乘客2", "lat":"30.753299067284", "lon":"120.48161327404"}];
var jpushdriver = {"key":"8fe07689d84f644ac617973b", "secret":"8bedebfc1a7e50b9b79cfcac"};
var jpushpassenger = {"key":"da542abb1dfae4ad09104f6b", "secret":"332043472c2c2f0cea927e7d"};
//address = []

var pushTerminal = function(appkey, masterSecret, message, title, name){
	var client = JPush.buildClient(appkey, masterSecret);

	//easy push
	client.push().setPlatform(JPush.ALL)
	    .setAudience(JPush.alias(name))
	    .setNotification(message, JPush.android(message, title, 1))
	    .send(function(err, res) {
	        if (err) {
	            console.log(err.message);
	        } else {
	            console.log('Sendno: ' + res.sendno);
	            console.log('Msg_id: ' + res.msg_id);
	        }
	    });
}

//passenger and driver's address
router.get('/toforeground', function(req, res){
	var address = [{"driver":driverAll},{"passenger":passengerAll}];
	res.jsonp(address);
});

//passenger
router.post('/getdriver', function(req, res){
	console.log("getdriver");
	var passengername = req.body.username;
	var driver_p = [];

	var ifdp = false;
	for(var d in driverAll){
		if(driverAll[d].hasOwnProperty("passenger") ){
			if(driverAll[d].passenger == passengername){
				var driverp = [];
				driverp.push(driverAll[d]);
				res.jsonp(driverp);
				//console.log(driverAll[d]);
				ifdp = true;
				break;
			}
		} else{
			driver_p.push(driverAll[d]);
		}
	};

	if(!ifdp){
		if(driver_p.length == 0){
			res.jsonp(driverAll);
		} else{
			res.jsonp(driver_p);
		}
	}
	
});

router.post('/postpassenger', function(req, res){
	console.log("postpassenger");
	var passengername = req.body.username;
	var passadd = {};
	var lat = req.body.lat + lat_;
	var lon = req.body.lon + lon_;
	passadd.passenger = passengername;
	passadd.lat = lat;
	passadd.lon = lon;

	var number = 0;
	for (var o in passengerAll){
		if(passengerAll[o].passenger == passengername){
			passengerAll[o].lat = lat;
			passengerAll[o].lon = lon;
		} else {
			number++;
		}
	};

	if(number == passengerAll.length){
		passengerAll.push(passadd);
	}
	
	res.jsonp({status:'ok'});
});

router.post('/passengercall', function(req, res){
	console.log("passengercall");
	var passengername = req.body.username;
	var call = "叫车";
	var message = passengername + call;

	var ifSuccess = false;
	for(var d in driverAll){
		if(driverAll[d].hasOwnProperty("passenger") ){
		} else{
			pushTerminal(jpushdriver.key, jpushdriver.secret, message, call, driverAll[d].driver);

			driverAll[d].passenger = passengername;

			ifSuccess = true;
			res.jsonp({status:'ok'});
			break;			
		}
	}

	if(ifSuccess == false){
		res.jsonp({status:'false'});
	}
});

router.post('/passengercancel', function(req, res){
	console.log("passengercancel");
	var passengername = req.body.username;

	var title = "取消叫车";
	var message = passengername + title;
	var ifSuccess = false;
	for(var d in driverAll){
		if(driverAll[d].hasOwnProperty("passenger") ){
			if(driverAll[d].passenger == passengername){
				pushTerminal(jpushdriver.key, jpushdriver.secret, message, title, driverAll[d].driver);
				console.log("4");
				delete driverAll[d].passenger;
				res.jsonp({status:'ok'});
				ifSuccess = true;
				break;	
			}
		} 
	}

	if(ifSuccess == false){
		res.jsonp({status:'false'});
	}

});


router.post('/getpassenger', function(req, res){
	console.log("getpassenger");
	var dirvername = req.body.username;

	var ifSuccess = false;
	for (var o in driverAll){
		if(driverAll[o].driver == dirvername){
			for (var p in passengerAll){
				if(driverAll[o].passenger == passengerAll[p].passenger){
					var pasend = [];
					pasend.push(passengerAll[p]);
					ifSuccess = true;
					res.jsonp(pasend);						
					break;
				}
			}
		}
			
	};

	if(ifSuccess == false){
		var passengerNull = [];
		res.jsonp(passengerNull);
	}
});



router.post('/postdriver', function(req, res){
	console.log("postdriver");
	var driverAdd = {};
	var dirvername = req.body.username;
	var lat = req.body.lat + lat_;
	var lon = req.body.lon + lon_;
	driverAdd.driver = dirvername;
	driverAdd.lat = lat;
	driverAdd.lon = lon;

	var ifold = false;
	for (var o in driverAll){
		if(driverAll[o].driver == dirvername){
			driverAll[o].lat = lat;
			driverAll[o].lon = lon;
			ifold = true;
			break;
		} 			
	};

	if(ifold == false){
		driverAll.push(driverAdd);
	}

	res.jsonp({status:'ok'});
});



router.post('/driveranswer', function(req, res){
	console.log("driveranswer");
	var dirvername = req.body.username;
	var title = "叫车成功";
	var message = dirvername + "接受叫车";

	var ifSuccess = false;
	for(var d in driverAll){
		if(driverAll[d].hasOwnProperty("passenger") ){
			if(driverAll[d].driver == dirvername){
				pushTerminal(jpushpassenger.key, jpushpassenger.secret, message, title, driverAll[d].passenger);
				ifSuccess = true;
				res.jsonp({status:'ok'});
				break;	
			}
		} 
	}

	if(ifSuccess == false){
		res.jsonp({status:'false'});
	}

});

router.post('/drivercancel', function(req, res){
	console.log("drivercancel");
	var dirvername = req.body.driver;

	var answer = "取消成功";
	var message = dirvername + answer;

	var ifSuccess = false;

	for(var d in driverAll){
		if(driverAll[d].hasOwnProperty("passenger") ){
			if(driverAll[d].driver == dirvername){
				pushTerminal(jpushpassenger[d].key, jpushpassenger[d].secret, message, answer, driverAll[d].passenger);
				delete driverAll[d].passenger;
				res.jsonp({status:'ok'});
				ifSuccess = true;
				break;
			}
		} 
	}

	if(ifSuccess == false){
		res.jsonp({status:'false'});
	}
});

module.exports = router;




